﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;

namespace UtfprLibrary.Windows.Forms {
    [ToolboxBitmap(typeof(ResourceFinder), "Resources.VSD_UtfprRadioButton.png")]
    [Designer("UtfprLibrary.Windows.Forms.ComponentModel.UtfprRadioButtonDesigner, UtfprLibrary.WindowsForms.Design")]
    public class UtfprRadioButton : UtfprRadioButtonBase {
        #region Properties

        private bool _isNumeric = false;
        [Category("Behavior")]
        [Description("")]
        public bool IsNumeric {
            get { return _isNumeric; }
            set {
                _isNumeric = value;
                OnPropertyChanged("IsNumeric");
            }
        }

        private string _value = "Insira um valor";
        [Category("Data")]
        [Description("")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprRadioButtonEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public string Value {
            get {
                if (UtfprControlBase.IsInDesignMode) {
                    return _value;
                } else {
                    if (Checked) {
                        return _value;
                    } else {
                        var control = Parent.Controls.OfType<UtfprRadioButton>()
                            .Where(ctrl => ctrl.Checked == true).FirstOrDefault();

                        if (control != null) {
                            return control.Value;
                        } else {
                            return "";
                        }
                    }
                }
            }
            set {
                _value = value;
                OnPropertyChanged("Value");
            }
        }

        private UtfprRadioButton _defaultChecked;
        [Browsable(false)]
        public UtfprRadioButton DefaultChecked {
            get {
                if (_defaultChecked == null) {
                    if (Parent.GetType() == typeof(UtfprGroupBox)) {
                        _defaultChecked = (Parent as UtfprGroupBox).DefaultChecked;
                        if (_defaultChecked != null) {
                            _defaultChecked.Checked = true;
                        }
                    }
                }
                return _defaultChecked;
            }
        }

        [Browsable(false)]
        [Bindable(true)]
        public object ValueField {
            get { return this.Value; }
            set {
                if (!UtfprControlBase.IsInDesignMode) {
                    if (value.ToString() == this._value) {
                        this.Checked = true;
                    } else if (CanUncheck(value)) {
                        if(DefaultChecked != null) {
                            DefaultChecked.Checked = true;
                        }
                    }
                }
            }
        }

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName) {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Methods

        private bool CanUncheck(object value) {
            return
                value == null ||
                value.ToString() == "" ||
                value.GetType() == typeof(DBNull);
        }

        #endregion
    }
}
